import { DD_XXL_JOB } from '@/constants/api-pre';
import { request } from '@/service/request';

/** ====================================== 获取xxl-job图表信息 ====================================== */
export type XxlJobChartInfo = {
  executorCount: number;
  triggerCountFailTotal: number;
  triggerCountSucTotal: number;
  triggerCountRunningTotal: number;
  triggerDayList: string[];
  triggerDayCountRunningList: number[];
  triggerDayCountFailList: number[];
  triggerDayCountSucList: number[];
};
export function fetchXxlJobChartInfo() {
  return request<XxlJobChartInfo>({
    url: `${DD_XXL_JOB}/chartInfo`,
    method: 'post'
  });
}

/** ====================================== 获取xxl-job分组列表 ====================================== */
export type XxlJobRegistry = {
  appname: string;
  status: number;
  address: string;
};
export type XxlJobGroup = Api.Common.CommonRecord<{
  appname: string;
  title: string;
  /** 0=自动注册、1=手动录入 */
  addressType: number;
  addressList: string;
  updateTime: string;
  registryList: XxlJobRegistry[];
}>;

export type XxlJobGroupEditAndCreateParams = CommonType.RecordNullable<
  Pick<XxlJobGroup, 'id' | 'appname' | 'title' | 'addressType' | 'addressList'>
>;

export type XxlJobGroupList = Api.Common.PaginatingQueryRecord<XxlJobGroup>;
export type XxlJobRegistryList = Api.Common.PaginatingQueryRecord<XxlJobRegistry>;

export type XxlJobGroupSearchParams = CommonType.RecordNullable<
  Pick<XxlJobGroup, 'appname' | 'title'> & Api.Common.CommonSearchParams
>;
export type XxlJobRegistrySearchParams = CommonType.RecordNullable<
  Pick<XxlJobGroup, 'id'> & Api.Common.CommonSearchParams
>;

export function getFetchXxlJobGroupUrl(id: number) {
  return `${DD_XXL_JOB}/job-group/${id}/form`;
}
export function fetchXxlJobGroupList(params?: XxlJobGroupSearchParams) {
  return request<XxlJobGroupList>({
    url: `${DD_XXL_JOB}/job-group/page`,
    method: 'get',
    params
  });
}
export function fetchXxlJobGroupOptions() {
  return request<CommonType.Option<number>[]>({
    url: `${DD_XXL_JOB}/job-group/options`,
    method: 'get'
  });
}
export function fetchXxlJobRegistryList(params?: XxlJobRegistrySearchParams) {
  return request<XxlJobRegistryList>({
    url: `${DD_XXL_JOB}/job-group/${params?.id}/registry`,
    method: 'get'
  });
}
export function fetchXxlJobGroup(id?: number) {
  return request<XxlJobGroup>({
    url: `${DD_XXL_JOB}/job-group/${id}/form`,
    method: 'get'
  });
}
export function createXxlJobGroup(data: any) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-group`,
    method: 'post',
    data
  });
}
export function updateXxlJobGroup(id: number, data?: any) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-group/${id}`,
    method: 'put',
    data
  });
}
export function deleteXxlJobGroup(id: number) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-group/${id}`,
    method: 'delete'
  });
}

/** ====================================== 获取xxl-job-info列表 ====================================== */
export type XxlJobInfo = Api.Common.CommonRecord<{
  jobGroup: number;
  jobGroupDesc: string;
  jobDesc: string;
  addTime: string;
  updateTime: string;
  author: string;
  alarmEmail: string;
  scheduleType: string;
  scheduleConf: string;
  triggerFrequency: string;
  executorMode: string;
  misfireStrategy: string;
  executorRouteStrategy: string;
  executorHandler: string;
  executorParam: string;
  executorBlockStrategy: string;
  executorTimeout: number;
  executorFailRetryCount: number;
  glueType: string;
  glueSource: string;
  glueRemark: string;
  glueUpdatetime: string;
  childJobId: string;
  /** 0-停止，1-运行 */
  triggerStatus: number;
  triggerLastTime: string;
  triggerNextTime: string;
}>;

export type XxlJobInfoEditAndCreateParams = CommonType.RecordNullable<
  Exclude<XxlJobInfo, 'updateTime' | 'addTime' | 'glueUpdatetime'>
>;

export type XxlJobInfoList = Api.Common.PaginatingQueryRecord<XxlJobInfo>;

export type XxlJobInfoSearchParams = CommonType.RecordNullable<
  Pick<XxlJobInfo, 'jobGroup' | 'triggerStatus' | 'jobDesc' | 'executorHandler' | 'author'> &
    Api.Common.CommonSearchParams
>;

export type Crontab = {
  second: string;
  min: string;
  hour: string;
  day: string;
  month: string;
  week: string;
  year: string;
};

export function fetchXxlJobInfoList(params?: XxlJobInfoSearchParams) {
  return request<XxlJobInfoList>({
    url: `${DD_XXL_JOB}/job-info/page`,
    method: 'get',
    params
  });
}
export function createXxlJobInfo(data: any) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-info`,
    method: 'post',
    data
  });
}
export function updateXxlJobInfo(id?: number, data?: any) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-info/${id}`,
    method: 'put',
    data
  });
}
export function deleteXxlJobInfo(id: string) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-info/${id}`,
    method: 'delete'
  });
}
export function triggerXxlJobInfo(id: number, executorParam: string, addressList: string) {
  return request<Boolean>({
    url: `${DD_XXL_JOB}/job-info/trigger`,
    method: 'post',
    params: {
      id,
      executorParam,
      addressList
    }
  });
}
export function nextTriggerTimeXxlJobInfo(scheduleType: string, scheduleConf: string) {
  return request<string[]>({
    url: `${DD_XXL_JOB}/job-info/nextTriggerTime`,
    method: 'post',
    params: {
      scheduleType,
      scheduleConf
    }
  });
}
export function startXxlJobInfo(id: number) {
  return request<string[]>({
    url: `${DD_XXL_JOB}/job-info/start`,
    method: 'post',
    params: {
      id
    }
  });
}
export function stopXxlJobInfo(id: number) {
  return request<string[]>({
    url: `${DD_XXL_JOB}/job-info/stop`,
    method: 'post',
    params: {
      id
    }
  });
}

/** ====================================== 调度日志 ====================================== */
export type XxlJobLog = Api.Common.CommonRecord<{
  id: number;

  jobGroup: number;
  jobId: number;

  // execute info
  executorAddress: string;
  executorHandler: string;
  executorParam: string;
  executorShardingParam: string;
  executorFailRetryCount: number;

  // trigger info
  triggerTime: string;
  triggerCode: number;
  triggerMsg: string;

  // handle info
  handleTime: string;
  handleCode: number;
  handleMsg: string;

  // alarm info
  alarmStatus: number;
}>;
export type XxlJobLogList = Api.Common.PaginatingQueryRecord<XxlJobLog>;
export type XxlJobLogSearchParams = CommonType.RecordNullable<
  Pick<XxlJobLog, 'jobGroup' | 'jobId'> & {
    logStatus: number;
    filterTime: null;
  } & Api.Common.CommonSearchParams
>;
export type XxlJobLogResult = Api.Common.CommonRecord<{
  fromLineNum: number;
  toLineNum: number;
  logContent: string;
  isEnd: boolean;
}>;

export function fetchXxlJobLogList(params?: XxlJobLogSearchParams) {
  return request<XxlJobLogList>({
    url: `${DD_XXL_JOB}/job-log/page`,
    method: 'get',
    params
  });
}
export function fetchJobsByGroup(jobGroup?: string) {
  return request<XxlJobInfo[]>({
    url: `${DD_XXL_JOB}/job-log/getJobsByGroup`,
    method: 'get',
    params: {
      jobGroup
    }
  });
}
export function fetchLogDetailCat(jobLogId?: number, fromLineNum?: number) {
  return request<XxlJobLogResult>({
    url: `${DD_XXL_JOB}/job-log/logDetailCat`,
    method: 'get',
    params: {
      logId: jobLogId,
      fromLineNum
    }
  });
}
export function killJob(jobLogId?: number) {
  return request<string>({
    url: `${DD_XXL_JOB}/job-log/logKill`,
    method: 'post',
    params: {
      id: jobLogId
    }
  });
}
